package com.nantai.jiafang.nantaijiafang.nt5s.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.nantai.jiafang.nantaijiafang.R;
import com.nantai.jiafang.nantaijiafang.ViewModel.Image_load;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Martin on 2017/1/18.
 */

public class PickAdapter extends BaseAdapter {

  private LayoutInflater layoutInflater;
  private List<String> data = new ArrayList<String>();
  private boolean isShowDelete;
  private Context context;

  public PickAdapter(Context context) {
    layoutInflater = LayoutInflater.from(context);
    this.context=context;
  }

  public void setNewData(List<String> data) {
    this.data.clear();
    if (data != null) {
      this.data.addAll(data);
    }
    notifyDataSetChanged();
  }
  public void add(String filepath){
    this.data.add(filepath);
    notifyDataSetChanged();
  }
  public void removeitem(int postion){
    this.data.remove(postion);
    notifyDataSetChanged();
  }
  public void setIsShowDelete(boolean isShowDelete) {
    this.isShowDelete = isShowDelete;
    notifyDataSetChanged();
  }


  @Override
  public int getCount() {
    return data.size();
  }

  @Override
  public Object getItem(int position) {
    return data.get(position);
  }

  @Override
  public long getItemId(int position) {
    return position;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    View view;
    if (convertView == null) {
      view = layoutInflater.inflate(R.layout.image_item, null);
      holder = new ViewHolder();
      holder.imageView = (ImageView) view.findViewById(R.id.image);
      holder.deleteView= (ImageView) view.findViewById(R.id.delete_markView);
      view.setTag(holder);
    } else {
      view=convertView;
      holder = (ViewHolder) view.getTag();
    }
    holder.deleteView.setVisibility(isShowDelete ? View.VISIBLE : View.GONE);
    Image_load.load(context,holder.imageView,data.get(position));
    return view;
  }
  public static byte[] getLoacalBitmap(String url) {
    try {
     // FileInputStream fis = new FileInputStream(url);

      return  upbitmap(url,10);  ///把流转化为Bitmap图片

    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
  }

  class ViewHolder {
    public ImageView imageView,deleteView;
  }
  public static byte[] upbitmap(String filepath,int m){
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = m;
    Bitmap bm = BitmapFactory.decodeFile(filepath, options);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.JPEG, 50, baos);
    byte[] data2 = baos.toByteArray();

 /*   int bytes = bm.getByteCount();
    ByteBuffer buf = ByteBuffer.allocate(bytes);
    bm.copyPixelsToBuffer(buf);
    byte[] byteArray = buf.array();*/
    Log.i("wechat", "压缩后图片的大小" + (bm.getByteCount() / 1024 / 1024)
            + "M宽度为" + bm.getWidth() + "高度为" + bm.getHeight());
        return data2;
    }
  }
